package org.example.controller;


import lombok.RequiredArgsConstructor;
import org.example.domain.ResponseResult;
import org.example.domain.dto.RoleListDto;
import org.example.domain.entity.Role;
import org.example.enums.AppHttpCodeEnum;
import org.example.exception.SystemException;
import org.example.service.RoleService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;


    @PostMapping("/roleList")
    public ResponseResult roleList(@RequestBody RoleListDto roleListDto){

        return roleService.roleList(roleListDto);
    }

    @PutMapping("/changeRoleStatus")
    public ResponseResult changeRoleStatus(@RequestBody Role role){
        return ResponseResult.okResult(roleService.changeRoleStatus(role));
    }

    @PostMapping("/addRole")
    public ResponseResult addRole(@RequestBody Role role){
        if(!StringUtils.hasText(role.getRoleName())){
            throw new SystemException(AppHttpCodeEnum.NOT_ROLE_NAME_ERROR);
        }

        if(!StringUtils.hasText(role.getRoleKey())){
            throw new SystemException(AppHttpCodeEnum.NOT_ROLE_KEY_ERROR);
        }

        return roleService.addRole(role);
    }

    @GetMapping("/getAllRoles")
    public ResponseResult getAllRoles(){

        return roleService.getAllRoles();
    }

    @PutMapping("/updateRole")
    public ResponseResult updateRole(@RequestBody Role role){

        return roleService.updateRole(role);
    }
}
